package com.littcore.saap.assistant.web;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.WebRequest;

import com.littcore.common.Utility;
import com.littcore.saap.assistant.dao.TagAssocDao;
import com.littcore.saap.assistant.po.Tag;
import com.littcore.saap.assistant.service.ITagService;
import com.littcore.saap.core.web.util.LoginUtils;

@Controller
public class TagController {
	
	@Resource
	private ITagService tagService;
	
	@Resource
	private TagAssocDao tagAssocDao;
	
	
	 @RequestMapping 
	  public void addTag(WebRequest request) throws Exception
	  { 
		String moduleCode= request.getParameter("moduleCode");
		String name=request.getParameter("name");
		int recordId=Utility.parseInt(request.getParameter("recordId"));
		String style=request.getParameter("style");
		Tag tag=new Tag();
		tag.setModuleCode(moduleCode);
		tag.setName(name);
		tag.setUserDefined(true);
		tag.setTenantId(LoginUtils.getTenantId());
		tag.setStyle(StringUtils.isEmpty(style)==true?"":style);
		
		tagService.saveTag(tag, recordId);
	   
	  }
	 @RequestMapping 
	 public void getTags(WebRequest request,ModelMap modelMap){
		 String moduleCode= request.getParameter("moduleCode");
		 int recordId=Utility.parseInt(request.getParameter("recordId"));
		 List tags=tagService.getTags(LoginUtils.getTenantId(), moduleCode, recordId);
		 modelMap.put("tags", tags);
	 }
	 @RequestMapping 
	 public void deleteTagAssoc(WebRequest request)throws Exception{
		int assocId=Utility.parseInt(request.getParameter("assocId")); 
		tagAssocDao.deleteById(assocId);

		
	 }
	 
	 @RequestMapping 
	 public void deleteTag(WebRequest request){
		int tagId=Utility.parseInt(request.getParameter("tagId")); 
	    tagService.deleteTag(tagId);
		
	 }

}
